#!/bin/sh

#Common functions

#$1: Program to check for existance; return: 0 if found (and program full path in $PROG), 1 if not found.
function checkProg
{
    checkProg_prog=$(which "$1" 2>/dev/null)
    if [ $? -eq 0 ]; then
	PROG=$checkProg_prog
	return 0
    else
	return 1
    fi
}

#$1: Library to check for existance; return: 0 if found, 1 if not found.
function checkLib
{
    pkg-config --exists "$1" 2>&1 1>/dev/null
    return $?
}

#$1: State description
function stateSet
{
    echo -ne "$1 "
}

#$1 (optional): State green end status
function stateGreen
{
    if [ $# == 1 ] ; then
	echo -e "\033[32m$1\033[0m"
    else
	echo -e "\033[32mOK\033[0m"
    fi
}

#$1 (optional): State red end status
function stateRed
{
    if [ $# == 1 ] ; then
	echo -e "\033[31m$1\033[0m"
    else
	echo -e "\033[31mFAIL\033[0m"
    fi
}

#Check the command and exit if failed
#$1: Command exit code
function checkCommand
{
    if [ "$1" -eq 0 ] ; then
	stateGreen
    else
	stateRed
	exit 1
    fi
}